// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store'
import iView from 'iview'
import i18n from '@/locale'
import config from '@/config'
import importDirective from '@/directive'
import {
  directive as clickOutside
} from 'v-click-outside-x'
import installPlugin from '@/plugin'
import './index.less'
import '@/assets/icons/iconfont.css'
import TreeTable from 'tree-table-vue'
import VOrgTree from 'v-org-tree'
import 'v-org-tree/dist/v-org-tree.css'
import {
  recursionRouter
} from '@/libs/util'
import authRouter from "@/router/authRouters"
import funcRouter from '@/router/funcRouter'
// 实际打包时应该不引入mock
/* eslint-disable */
// if (process.env.NODE_ENV !== 'production') require('@/mock')

Vue.use(iView, {
  i18n: (key, value) => i18n.t(key, value)
})
Vue.use(TreeTable)
Vue.use(VOrgTree)
/**
 * @description 注册admin内置插件
 */
installPlugin(Vue)
/**
 * @description 生产环境关掉提示
 */
Vue.config.productionTip = false
/**
 * @description 全局注册应用配置
 */
Vue.prototype.$config = config
/**
 * 注册指令
 */
importDirective(Vue)
Vue.directive('clickOutside', clickOutside)

/**按钮权限指令**/
Vue.directive('perms', {
  bind: function (el, binding, vnode) {
    if (!Vue.prototype.$_perms(binding.value, vnode)) {
      el.parentNode.removeChild(el);
    }
  }
});
//权限检查方法
Vue.prototype.$_perms = function (value, vnode) {
  let isExist = false;
   let btnPermissions = sessionStorage.getItem("permissions");
  if (!btnPermissions) {
    return false;
  }
   let buttonperms = JSON.parse(btnPermissions);
  buttonperms.indexOf(value) > -1 ? isExist = true : isExist = false;
  return isExist;
};

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  i18n,
  store,
  render: h => h(App),
  created() {
    // 解决刷新 store 状态丢失 和获取左边的menu丢失
    this.$store.commit('refreshUserInfo');
    let menus = JSON.parse(sessionStorage.getItem('menu'))
    if (menus) {
      let realRoutes = recursionRouter(authRouter, menus);
      this.$router.addRoutes(realRoutes)
      this.$router.addRoutes(funcRouter)
      this.$store.commit('updateMenuList', realRoutes);
    }
  }
})
